//
//  WFCarousel.m
//  Carousel
//
//  Created by xalo on 16/4/14.
//  Copyright © 2016年 王朋飞. All rights reserved.



#import "WFCarousel.h"
#define kWIDTH  self.frame.size.width
#define kHEIGHT self.frame.size.height

@interface WFCarousel () <UIScrollViewDelegate>

@property(nonatomic,retain) UIScrollView *scrollView;
@property(nonatomic,assign) NSInteger index;
@property(nonatomic,retain) NSArray *pictureArray;
@property(nonatomic,retain) NSTimer *timer;
@property(nonatomic,retain) UIPageControl *pageControl;


@end

@implementation WFCarousel


- (instancetype)initWithFrame:(CGRect)frame pictureArray:(NSArray*)pictureArray{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.index = 0 ;
        self.pictureArray = pictureArray ;
        [self addSubview:self.scrollView];
        [self addSubview:self.pageControl];
        //添加图片
        [self addImagViewToScrollView];
        [self initTimer];
    }
    return self;
    
}


//初始化计时器
- (void)initTimer{
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeOffset) userInfo:nil repeats:YES];
}

- (void)changeOffset{
    
    [self.scrollView setContentOffset:CGPointMake(2 * kWIDTH, 0) animated:YES];
}

//初始化滚动视图
- (UIScrollView *)scrollView{
    
    if (!_scrollView) {
        
        _scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        //设置划动范围
        _scrollView.contentSize = CGSizeMake(kWIDTH * 3, kHEIGHT);
        _scrollView.contentOffset = CGPointMake(kWIDTH, 0);
       
        [self addSubview:_scrollView];
    }
    return _scrollView;
}

//初始化pageControl
- (UIPageControl*)pageControl{
    
    if (!_pageControl) {
        
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, kWIDTH, 30)];
        _pageControl.numberOfPages = self.pictureArray.count;
        CGPoint point = _pageControl.center;
        point.x = self.center.x;
        point.y = kHEIGHT - 15;
        _pageControl.center = point;
      
    }
    return _pageControl;
}

//开始拖，停止计时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.timer invalidate];
    //失效后必须置nil
     self.timer = nil;
    
    /**
     *  [self.timer setFireDate:[NSDate distantFuture]];
     *  [self.timer setFireDate:[NSDate distantPast]];
     *  [self performSelector:@selector(openTimer) withObject:nil afterDelay:2 ];
     */
    
}

//停止拖，开始计时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [self initTimer];
}

//添加初始图片到滚动视图
- (void)addImagViewToScrollView{
    
    for (int i = 0; i < 3; i ++) {
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(kWIDTH * i , 0, kWIDTH, kHEIGHT)];
       
            if (i == 0) {
                
                imageView.image = self.pictureArray.lastObject;
            } else if ( i == 1){
                
                imageView.image = self.pictureArray.firstObject;
            }else{
                
                imageView.image = self.pictureArray[1];
            }
       
        imageView.tag = 1000 + i;
        imageView.contentMode = UIViewContentModeScaleToFill;
        [self.scrollView addSubview:imageView];
    }
    
}

#pragma mark - scrollView的代理方法


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    if (self.scrollView.contentOffset.x >= 2 * kWIDTH) {
        
        if (self.index == self.pictureArray.count - 1) {
            
            self.index = 0;
        }else{
            
            self.index++;
            
        }
    }
    //重新设置照片
    [self addImageWithIndex:(NSInteger)self.index];
    
    //将scrollView置回原位
    [scrollView setContentOffset:CGPointMake(kWIDTH, 0) animated:NO];
    
    //设置小白点
    self.pageControl.currentPage = self.index;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //向右侧划动
    if (self.scrollView.contentOffset.x >=  kWIDTH) {
        
        if (self.index == self.pictureArray.count - 1) {
        
            self.index = 0;
        }else{
            
            self.index++;
            
        }
        
    //向左侧划动
    }else if (self.scrollView.contentOffset.x < kWIDTH){
      
        if (self.index == 0) {
            
            self.index = self.pictureArray.count -1;
        }else{
            
            self.index--;
        }
        
    }
    
    //重新设置照片
    [self addImageWithIndex:(NSInteger)self.index];
    //将scrollView置回原位
    [scrollView setContentOffset:CGPointMake(kWIDTH, 0) animated:NO];
    
    //设置小白点
    self.pageControl.currentPage = self.index;

}
- (void)addImageWithIndex:(NSInteger)index{
    
    UIImageView *imageView1 = (UIImageView*)[self viewWithTag:1000];
    UIImageView *imageView2 = (UIImageView*)[self viewWithTag:1001];
    UIImageView *imageView3 = (UIImageView*)[self viewWithTag:1002];
    
    
    if (index == 0) {
        
        imageView1.image = self.pictureArray.lastObject;
        imageView2.image = self.pictureArray.firstObject;
        imageView3.image = self.pictureArray[1];
    } else if (index == self.pictureArray.count - 1){
        
        imageView1.image = self.pictureArray[self.pictureArray.count -2];
        imageView2.image = self.pictureArray[self.pictureArray.count -1];
        imageView3.image = self.pictureArray[0];
    } else{
        
        imageView1.image = self.pictureArray[index -1];
        imageView2.image = self.pictureArray[index];
        imageView3.image = self.pictureArray[index +1];
    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
